<template>
  <div class="layout">
    <!-- 详情页顶部 -->
    <div class="detail-head">
      <!-- 遮罩层 -->
      <div class="mask" :style="{ opacity: headopc }">
        <div class="maskbg" :style="`background-image:url('${src}')`"></div>
        <div class="back-bg"></div>
        <span class="detail-name">{{ name }}</span>
      </div>
      <!-- 返回按钮 -->
      <div @click="back" class="back">
        <img src="@/assets/img/ui/arrow_left.png" />
      </div>
    </div>

    <!-- 歌手信息 -->
    <div class="artist">
      <app-load v-show="!isArtistLoad"></app-load>
      <img v-show="isArtistLoad" class="art-img" v-lazy="src" alt="Loading" />
      <h1 class="art-name">{{ name }}</h1>
    </div>

    <!-- 歌曲列表 -->
    <div class="songs">
      <ul class="song-list">
        <span class="list-title">热门歌曲</span>
        <app-load v-show="!isSongLoad"></app-load>
        <li v-for="(item, index) in songsList" :key="index">
          <router-link :to="{ path: '/detail/songs', query: { id: item.id } }">
            <div class="song-rank">{{ index + 1 }}</div>
            <div class="song-img">
              <img v-lazy="item.src" alt="Loading" />
            </div>
            <div class="song-name">
              <span>{{ item.name }}</span>
            </div>
          </router-link>
        </li>
      </ul>
    </div>

    <!-- 专辑列表 -->
    <div class="ablums">
      <ul class="ablum-list">
        <span class="list-title">热门专辑</span>
        <app-load v-show="!isAlbumLoad"></app-load>
        <li v-for="(item, index) in albumList" :key="index">
          <div class="ablum-img">
            <img v-show="isAlbumLoad" v-lazy="item.src" alt="Loading" />
          </div>
          <div class="ablum-name">
            <span>{{ item.name }}</span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import appLoad from "@/components/app-load.vue";
export default {
  name: "artists",
  data() {
    return {
      headopc: "", // 透明度
      artistId: "",
      name: "",
      src: "",
      id: "",
      songsList: [],
      albumList: [],
      isArtistLoad: false, // 判断歌手图片是否加载
      isSongLoad: false, // 判断歌曲图片是否加载
      isAlbumLoad: false, // 判断专辑图片是否加载
    };
  },
  methods: {
    // 获取数据
    getData() {
      // 获取歌手信息
      axios.get("/artists", { params: { id: this.artistId } }).then((data) => {
        if (data.code == 200) {
          this.name = data.artist.name;
          this.src = data.artist.picUrl;
          this.id = data.artist.id;
          // 判断是否加载
          this.isArtistLoad = true;
        }
      });
      // 获取歌曲
      axios
        .get("/artist/top/song", { params: { id: this.artistId } })
        .then((data) => {
          if (data.code == 200) {
            data.songs.forEach((item) => {
              let obj = {};
              obj.name = item.name;
              obj.src = item.al.picUrl;
              obj.id = item.id;
              this.songsList.push(obj);
              // 判断是否加载
              this.isSongLoad = true;
            });
          }
        });
      // 获取专辑
      axios
        .get("/artists/album", { params: { id: this.artistId } })
        .then((data) => {
          if (data.code == 200) {
            data.hotSongs.forEach((item) => {
              let obj = {};
              obj.name = item.al.name;
              obj.src = item.al.picUrl;
              obj.id = item.id;
              this.albumList.push(obj);
              // 判断是否加载
              this.isAlbumLoad = true;
            });
          }
        });
    },
    // 滚动透明度
    scrollHead() {
      let scrollV =
        document.documentElement.scrollTop || document.body.scrollTop;
      let scrollH = 100;
      if (scrollV <= scrollH) {
        this.headopc = (scrollV / scrollH) * 1;
      } else {
        this.headopc = 1;
      }
    },
    // 返回上一页
    back() {
      this.$router.go(-1);
    },
  },
  created() {
    this.artistId = this.$route.query.id;
    this.getData();
    addEventListener("scroll", this.scrollHead);
  },
  components: {
    appLoad,
  },
};
</script>

<style lang="scss" scoped>
.layout {
  height: 100%;
  overflow: hidden;
  color: white;
  // 详情页顶部
  .detail-head {
    width: 100%;
    height: 80px;
    position: fixed;
    top: 0;
    z-index: 10;
    // 顶部遮罩层
    .mask {
      width: 100%;
      height: 100%;
      opacity: 0;
      display: flex;
      justify-content: center;
      align-items: end;
      position: absolute;
      top: 0;
      overflow: hidden;
      span {
        text-shadow: 0 0 2px black;
        margin-bottom: 20px;
        position: relative;
      }
      .maskbg {
        width: 1000%;
        height: 1000%;
        position: absolute;
        top: -500px;
        left: -1000px;
        filter: blur(38px);
      }
      .back-bg {
        width: 28px;
        height: 28px;
        position: absolute;
        left: 20px;
        bottom: 20px;
        border-radius: 50%;
        background-color: rgba(126, 126, 126, 0.6);
      }
    }
    // 返回按钮
    .back {
      width: 28px;
      height: 28px;
      position: absolute;
      left: 20px;
      bottom: 20px;
      border-radius: 50%;
      overflow: hidden;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 68%;
      }
    }
  }
  // 歌手信息
  .artist {
    width: 100%;
    height: 250px;
    position: relative;
    // 歌手图片
    .art-img {
      width: 100%;
      height: 100%;
      // background-color: springgreen;
    }
    // 歌手名字
    .art-name {
      width: 100%;
      font-size: 50px;
      text-shadow: 0 0 8px black;
      position: absolute;
      left: 15px;
      bottom: 2px;
    }
  }
  // 歌曲列表
  .songs {
    width: 100%;
    margin-top: 20px;
    .song-list {
      .list-title {
        height: 38px;
        line-height: 38px;
        margin-left: 15px;
        display: block;
      }
      li {
        margin-bottom: 15px;
        a {
          width: 100%;
          color: white;
          display: flex;
          .song-rank {
            width: 20px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            margin-left: 15px;
            margin-right: 10px;
          }
          .song-img {
            width: 50px;
            height: 50px;
            margin-right: 10px;
            img {
              width: 100%;
              height: 100%;
              // background-color: springgreen;
            }
          }
          .song-name {
            width: calc(100% - 15px - 20px - 10px - 50px - 10px);
            height: 50px;
            padding-right: 15px;
            padding-top: 10px;
            box-sizing: border-box;
            span {
              font-size: 16px;
              display: block;
              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
            }
          }
        }
      }
    }
  }
  // 专辑列表
  .ablums {
    width: 100%;
    margin-top: 50px;
    margin-bottom: 100px;
    .ablum-list {
      .list-title {
        height: 38px;
        line-height: 38px;
        margin-left: 15px;
        display: block;
      }
      li {
        width: 100%;
        display: flex;
        margin-bottom: 15px;
        .ablum-img {
          width: 80px;
          height: 80px;
          flex-shrink: 0;
          margin-left: 15px;
          margin-right: 10px;
          img {
            width: 100%;
            height: 100%;
            // background-color: springgreen;
          }
        }
        .ablum-name {
          height: 80px;
          line-height: 80px;
          flex-grow: 1;
          overflow: hidden;
          span {
            text-overflow: ellipsis;
          }
        }
      }
    }
  }
}
</style>